package org.jeecg.modules.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.ding.DingApi;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.jsonschema.JsonschemaUtil;
import org.jeecg.modules.entity.*;
import org.jeecg.modules.service.*;
import org.jeecg.modules.service.impl.DiyProjectProfessorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

@Api(tags = "专家分数录入")
@RestController
@RequestMapping("/org/diyProfessorInfo2")
@Slf4j
public class DiyProfessorScoreController  extends JeecgController<DiyProfessorScore, DiyProfessorScoreService> {

    @Autowired
    private  DiyProfessorScoreService diyProfessorScoreService;
    @Autowired
    private DiyProfessiorPointService diyProfessiorPointService;
    @Autowired
    private IDiyProjectProfessorService iDiyProjectProfessorService;

    @Autowired
    private DingApi dingApi;


    @AutoLog(value = "专家分数录入-分页列表查询")
    @ApiOperation(value = "专家分数录入-分页列表查询", notes = "专家分数录入-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(DiyProfessorScore diyProfessorScore,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        //总分
        System.out.println("前端？");
        System.out.println(diyProfessorScore);
        List<DiyProfessorScore> diyProfessorScores = diyProfessorScoreService.getBaseMapper().selectList(null);
        System.out.println("输出分数"+diyProfessorScores);
        diyProfessorScores.forEach(diyProfessorScore1 -> {
            Double firstScore = (double)diyProfessorScore1.getFirstScore();
            Double technicalTitleScore =(double) diyProfessorScore1.getTechnicalTitleScore();
            Double itemScore = (double)diyProfessorScore1.getItemScore();
//            System.out.println(firstScore);
            Double a =firstScore*0.3+technicalTitleScore*0.2+itemScore*0.5;
            if (a<0){
                a=0.0;
            }
//            System.out.println(a);
            UpdateWrapper<DiyProfessorScore> wrapper = new UpdateWrapper<>();
            wrapper.set("abc_score",a).eq("ding_user_id",diyProfessorScore1.getDingUserId());
            diyProfessorScoreService.getBaseMapper().update(null,wrapper);
        });
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy ");
        Page<DiyProfessorScore> page = new Page<>(pageNo, pageSize);
        System.out.println(page);
        System.out.println(diyProfessorScore);
        IPage<DiyProfessorScore> pageList = diyProfessorScoreService.pageByEntity(page, diyProfessorScore);
        System.out.println(pageList.toString());
        return Result.ok(pageList);
    }
    @AutoLog(value = "职业职称分数更改-添加")
    @ApiOperation(value = "职业职称分数更改-添加", notes = "职业职称分数更改-添加")
    @PostMapping(value = "/add")
    public Result<?> add( @RequestBody DiyProfessiorPoint diyProfessiorPoint) {
        UpdateWrapper<DiyProfessiorPoint> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id","1");
        diyProfessiorPoint.setId("1");
        diyProfessiorPointService.getBaseMapper().updateById(diyProfessiorPoint);

        return Result.ok(200);
    }
    @AutoLog(value = "设置初试分数-添加")
    @ApiOperation(value = "设置初试分数", notes = "设置初试分数-添加")
    @PostMapping(value = "/add2")
    public Result<?> add2( @RequestBody DiyProfessorScore diyProfessorScore) {
        UpdateWrapper<DiyProfessorScore> updateWrapper = new UpdateWrapper<>();
        System.out.println(diyProfessorScore.getProfessorName());
        System.out.println(diyProfessorScore.getFirstScore());
        updateWrapper.eq("professor_name",diyProfessorScore.getProfessorName()).set("first_score", diyProfessorScore.getFirstScore());
        diyProfessorScoreService.getBaseMapper().update(null, updateWrapper);
        return Result.ok(200);
    }
    @AutoLog(value = "设置专业职务-添加")
    @ApiOperation(value = "设置专业职务", notes = "设置专业职务-添加")
    @PostMapping(value = "/add3")
    public Result<?> add3( @RequestBody DiyProfessorScore diyProfessorScore) {
        UpdateWrapper<DiyProfessorScore> updateWrapper = new UpdateWrapper<>();
        System.out.println(diyProfessorScore.getProfessorName());
        System.out.println(diyProfessorScore.getProfessorTitle());
        updateWrapper.eq("professor_name", diyProfessorScore.getProfessorName()).set("professor_title", diyProfessorScore.getProfessorTitle());
        diyProfessorScoreService.getBaseMapper().update(null, updateWrapper);
        List<DiyProfessiorPoint> diyProfessiorPoints = diyProfessiorPointService.getBaseMapper().selectList(null);
        UpdateWrapper<DiyProfessorScore> wrapper = new UpdateWrapper<>();
        if (diyProfessorScore.getProfessorTitle().equals("国家级专家")) {
            wrapper.set("technical_title_score", Integer.parseInt(diyProfessiorPoints.get(0).getPointA())).eq("professor_name", diyProfessorScore.getProfessorName());
            diyProfessorScoreService.getBaseMapper().update(null, wrapper);
        } else if (diyProfessorScore.getProfessorTitle().equals("省级专家")) {
            wrapper.set("technical_title_score", Integer.parseInt(diyProfessiorPoints.get(0).getPointB())).eq("professor_name", diyProfessorScore.getProfessorName());
            diyProfessorScoreService.getBaseMapper().update(null, wrapper);
        } else if (diyProfessorScore.getProfessorTitle().equals("正高")) {
            wrapper.set("technical_title_score", Integer.parseInt(diyProfessiorPoints.get(0).getPointC())).eq("professor_name", diyProfessorScore.getProfessorName());
            diyProfessorScoreService.getBaseMapper().update(null, wrapper);
        } else if (diyProfessorScore.getProfessorTitle().equals("副高")) {
            wrapper.set("technical_title_score", Integer.parseInt(diyProfessiorPoints.get(0).getPointD())).eq("professor_name", diyProfessorScore.getProfessorName());
            diyProfessorScoreService.getBaseMapper().update(null, wrapper);
        }
        if (diyProfessorScore.getProfessorTitle().equals("中级")) {
            wrapper.set("technical_title_score", Integer.parseInt(diyProfessiorPoints.get(0).getPointE())).eq("professor_name",diyProfessorScore.getProfessorName());
            diyProfessorScoreService.getBaseMapper().update(null, wrapper);
        } else if (diyProfessorScore.getProfessorTitle().equals("初级")) {
            wrapper.set("technical_title_score", Integer.parseInt(diyProfessiorPoints.get(0).getPointF())).eq("professor_name", diyProfessorScore.getProfessorName());
            diyProfessorScoreService.getBaseMapper().update(null, wrapper);
    }
        return Result.ok(200);
    }



    @AutoLog(value = "专家分数录入-通过id删除")
    @ApiOperation(value = "专家分数录入-通过id删除", notes = "专家分数录入-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        diyProfessorScoreService.removeById(id);
        return Result.ok("删除成功!");
    }



    @AutoLog(value = "专家分数录入-批量删除")
    @ApiOperation(value = "专家分数录入-批量删除", notes = "专家分数录入-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.diyProfessorScoreService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }



    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DiyProfessorScore diyProfessorScore) {
        return super.exportXls(request, diyProfessorScore, DiyProfessorScore.class, "专家分数录入");
    }



    @RequestMapping(value = "/importExcel2", method = RequestMethod.POST)
    public Result<?> importExcel2(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("———————————————————————导入成功—————————————————————————");
        return super.importExcel(request, response, DiyProfessorScore.class);
    }


  /*   @RequestMapping(value = "/setScore",method = RequestMethod.PUT)
    public Result<?> setScore(DiyProfessiorPoint diyProfessiorPoint){
        //只更新一个属性，其他属性不变
        UpdateWrapper<DiyProfessiorPoint> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("title",diyProfessiorPoint.getTitle()).set("point", diyProfessiorPoint.getPoint());
        diyProfessiorPointService.getBaseMapper().update(null, updateWrapper);
        return Result.ok("更改成功");
    }*/

}
